import common.CommonDemo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.time.Duration;

public class ActionBuilder {

    private CommonDemo commonDemo;
    private ChromeDriver driver;
    private String url;

    @BeforeTest
    public void setUp() {
        try {
            commonDemo = new CommonDemo();
            driver = commonDemo.getChromeDriver();
            url = commonDemo.getUserDir("src/main/resources/actionBuilder.html");
            driver.get(url);
            driver.manage().window().maximize();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void actionPause() {
        WebElement element = driver.findElement(By.id("action"));
        Actions actions = new Actions(driver);
        actions.moveToElement(element)
                .pause(Duration.ofSeconds(2))
                .clickAndHold()
                .pause(Duration.ofSeconds(2))
                .sendKeys("测试一下")
                .perform();
    }

    @Test
    public void releaseAllActions(){
        actionPause();
//        ((RemoteWebDriver)driver).resetInputState();
        driver.resetInputState();
    }

    /**
     * 调用sendKeys(WebElement target, CharSequence... keys) ，指定需要输入文本的元素
     */
    @Test
    public void designatedElement(){
        WebElement element = driver.findElement(By.id("action"));
        new Actions(driver)
                .sendKeys(element, "演示")
                .perform();
    }



    @AfterTest
    public void tearDown() {
        try {
            Thread.sleep(2000);
            driver.quit();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
